/*

  Simple cross-product, outputs in correct triangular form.

  - Memory reads are float4s, half cooalescing
  - No memory coalescing in writes
  - All threads read in all data required
  - Each thread works on a 2x2 square of data

 */

CUBE_KERNEL(simple1x2, cuFloatComplex *array_d, cuFloatComplex *product_d, 
	    const int write)
{
  CUBE_START;

  //set frequency number from blockIdx.y
  int f = blockIdx.y;

  int Row, Col, blockX, blockY;
  CUBE_DEVICE_CALL(findPosition, Col, Row, blockX, blockY);
  Col*=2;

  //instantiate sum variables
  float sum11XXreal = 0.0, sum11XXimag = 0.0;
  float sum11XYreal = 0.0, sum11XYimag = 0.0;
  float sum11YXreal = 0.0, sum11YXimag = 0.0;
  float sum11YYreal = 0.0, sum11YYimag = 0.0;
  float sum12XXreal = 0.0, sum12XXimag = 0.0;
  float sum12XYreal = 0.0, sum12XYimag = 0.0;
  float sum12YXreal = 0.0, sum12YXimag = 0.0;
  float sum12YYreal = 0.0, sum12YYimag = 0.0;

  //  #pragma unroll 50
  for(int t=0; t<NTIME; t++){
    float4 col1 = ((float4*)array_d)[t*NFREQUENCY*NSTATION + f*NSTATION + Col];
    float4 col2 = ((float4*)array_d)[t*NFREQUENCY*NSTATION + f*NSTATION + Col + 1];
    float4 row1 = ((float4*)array_d)[t*NFREQUENCY*NSTATION + f*NSTATION + Row];
    CUBE_ADD_BYTES(48);

    sum11XXreal += row1.x * col1.x;
    sum11XXreal += row1.y * col1.y;
    sum11XXimag += row1.y * col1.x;
    sum11XXimag -= row1.x * col1.y;
    sum11XYreal += row1.x * col1.z;
    sum11XYreal += row1.y * col1.w;
    sum11XYimag += row1.y * col1.z;
    sum11XYimag -= row1.x * col1.w;
    sum11YXreal += row1.z * col1.x;
    sum11YXreal += row1.w * col1.y;
    sum11YXimag += row1.w * col1.x;
    sum11YXimag -= row1.z * col1.y;
    sum11YYreal += row1.z * col1.z;
    sum11YYreal += row1.w * col1.w;
    sum11YYimag += row1.w * col1.z;
    sum11YYimag -= row1.z * col1.w;
    sum12XXreal += row1.x * col2.x;
    sum12XXreal += row1.y * col2.y;
    sum12XXimag += row1.y * col2.x;
    sum12XXimag -= row1.x * col2.y;
    sum12XYreal += row1.x * col2.z;
    sum12XYreal += row1.y * col2.w;
    sum12XYimag += row1.y * col2.z;
    sum12XYimag -= row1.x * col2.w;
    sum12YXreal += row1.z * col2.x;
    sum12YXreal += row1.w * col2.y;
    sum12YXimag += row1.w * col2.x;
    sum12YXimag -= row1.z * col2.y;
    sum12YYreal += row1.z * col2.z;
    sum12YYreal += row1.w * col2.w;
    sum12YYimag += row1.w * col2.z;
    sum12YYimag -= row1.z * col2.w;

    /*    sum11XXreal += row1.x * col1.x + row1.y * col1.y;
    sum11XXimag += row1.y * col1.x - row1.x * col1.y;
    sum11XYreal += row1.x * col1.z + row1.y * col1.w;
    sum11XYimag += row1.y * col1.z - row1.x * col1.w;
    sum11YXreal += row1.z * col1.x + row1.w * col1.y;
    sum11YXimag += row1.w * col1.x - row1.z * col1.y;
    sum11YYreal += row1.z * col1.z + row1.w * col1.w;
    sum11YYimag += row1.w * col1.z - row1.z * col1.w;

    sum12XXreal += row1.x * col2.x + row1.y * col2.y;
    sum12XXimag += row1.y * col2.x - row1.x * col2.y;
    sum12XYreal += row1.x * col2.z + row1.y * col2.w;
    sum12XYimag += row1.y * col2.z - row1.x * col2.w;
    sum12YXreal += row1.z * col2.x + row1.w * col2.y;
    sum12YXimag += row1.w * col2.x - row1.z * col2.y;
    sum12YYreal += row1.z * col2.z + row1.w * col2.w;
    sum12YYimag += row1.w * col2.z - row1.z * col2.w;*/
  } 

  if (write)
    CUBE_DEVICE_CALL(write1x2, Col, Row, product_d, sum11XXreal, sum11XXimag, sum11XYreal, 
		     sum11XYimag, sum11YXreal, sum11YXimag, sum11YYreal, sum11YYimag, 
		     sum12XXreal, sum12XXimag, sum12XYreal, sum12XYimag, sum12YXreal, 
		     sum12YXimag, sum12YYreal, sum12YYimag);

  if (Col < Row) {
    CUBE_ADD_BYTES(64);
    CUBE_ADD_FLOPS(NTIME*64);
  } else {
    CUBE_ADD_BYTES(32);
    CUBE_ADD_FLOPS(NTIME*32);
  }

  CUBE_END;
}
